const router = require('koa-router')();
const { exec } = require('../db/index');

router.prefix('/api') // 统一添加路由前缀


router.get('/user', async (ctx, next) => {
  let data = await exec('select * from users');
  ctx.body = {
    code: 0,
    msg: 'success',
    data
  }
})

router.get('/page', async (ctx, next) => {
  const { page = 0, size = 5 } = ctx.query;
  // 5 0
  // 5 1 * 5 = 5;
  // 5 2 * 5 = 10;
  // 查询分页总数
  let total = await exec('select count(*) as num from users;')
  let data = await exec(`select * from users limit ${size} offset ${page * size}`);
  ctx.body = {
    code: 0,
    msg: 'success',
    total: total[0].num,
    data
  }
})


module.exports = router;